﻿Imports VN.INS.BL.BC
Imports System.Data

Partial Class Devida_Avance_pac
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            'Capturamos parametros y llenamos cabecera
            lblpac.Text = Request.QueryString("Nombre").ToString
            Dim nombre As String = Request.QueryString("Nombre").ToString
            lblPPER.Text = Request.QueryString("PPER").ToString
            ViewState("ID_PAC") = Request.QueryString("ID_PAC").ToString
            CargarFechas()
            CargarSemaforo()
            lbl_periodo.Text = Request.QueryString("PERIODO").ToString
            ViewState("PERIODO") = Request.QueryString("PERIODO2").ToString
            ViewState("NUEVO") = Request.QueryString("Nuevo").ToString
            If ViewState("NUEVO") = "M" Then    ' Si es edicion se cargan los datos corrspondientes
                ddlSemaforo.SelectedValue = Request.QueryString("SEM").ToString
                'Pintamos cuadro de semaforo segun sea el valor
                If ddlSemaforo.SelectedValue <> "" Then
                    If ddlSemaforo.SelectedValue = "1" Then
                        txtColor.BackColor = Drawing.Color.Green
                    ElseIf ddlSemaforo.SelectedValue = "2" Then
                        txtColor.BackColor = Drawing.Color.Orange
                    ElseIf ddlSemaforo.SelectedValue = "3" Then
                        txtColor.BackColor = Drawing.Color.Red
                    ElseIf ddlSemaforo.SelectedValue = "4" Then
                        txtColor.BackColor = Drawing.Color.White
                    End If
                End If
                txtObs.Text = Request.QueryString("OBSERV").ToString
                txtAcciones.Text = Request.QueryString("ACCION").ToString
                ViewState("SEG_PAC") = Request.QueryString("SEG_PAC").ToString
                MostrarFechas(ViewState("SEG_PAC"))
            End If
        End If
    End Sub

    'Carga descripciones de fecha PAC
    Protected Sub CargarFechas()
        Dim objDt As DataTable
        Dim objPAC As PacBC
        Try
            objDt = New DataTable
            objPAC = New PacBC
            objDt = objPAC.CargarFechaPAC(Session("Año"))
            gvFechas.DataSource = objDt
            gvFechas.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    'Llena fechas guardadas 
    Protected Sub MostrarFechas(ByVal strSeg As String)
        Dim objDt As DataTable
        Dim objPAC As PacBC
        Try
            objDt = New DataTable
            objPAC = New PacBC
            'Carga fechas correspondiente al PAC 
            objDt = objPAC.MostrarFechas(strSeg)
            For i As Int16 = 0 To gvFechas.Rows.Count - 1   'Recorremos grilla
                For j As Int16 = 0 To objDt.Rows.Count - 1
                    Dim hfCodigo As HiddenField = CType(gvFechas.Rows(i).FindControl("hfCodigo"), HiddenField)
                    Dim txt As TextBox = CType(gvFechas.Rows(i).FindControl("txtFecha"), TextBox)
                    Dim img As ImageButton = CType(gvFechas.Rows(i).FindControl("ImgCal"), ImageButton)
                    If hfCodigo.Value.ToString = objDt.Rows(j).Item("ID_FECHA").ToString Then   'Si codigos coinciden se llena la fecha
                        txt.Text = objDt.Rows(j).Item("FECHA").ToString
                        txt.Enabled = False
                        img.Enabled = False
                    End If
                Next
            Next
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    'Carga de datos de combo semaforo
    Public Sub CargarSemaforo()
        Dim objGeneral As GeneralBC
        Dim objDt As DataTable
        Try
            objGeneral = New GeneralBC
            objDt = New DataTable
            objDt = objGeneral.ListaTablaGeneral("SEMAFORO")
            ddlSemaforo.DataSource = objDt
            ddlSemaforo.DataTextField = "DESCRIPCION"
            ddlSemaforo.DataValueField = "CODIGO"
            ddlSemaforo.DataBind()
            ddlSemaforo.SelectedValue = "4"
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub ddlSemaforo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlSemaforo.SelectedIndexChanged
        If ddlSemaforo.SelectedValue = "1" Then         ' si se selecciona valor 1 pintamos el cuadro de verde
            txtColor.BackColor = Drawing.Color.Green
        ElseIf ddlSemaforo.SelectedValue = "2" Then     ' si se selecciona valor 2 pintamos el cuadro de ambar
            txtColor.BackColor = Drawing.Color.Orange
        ElseIf ddlSemaforo.SelectedValue = "3" Then     ' si se selecciona valor 3 pintamos el cuadro de rojo
            txtColor.BackColor = Drawing.Color.Red
        ElseIf ddlSemaforo.SelectedValue = "4" Then     ' si se selecciona valor 4 pintamos el cuadro de blanco (ninguno)
            txtColor.BackColor = Drawing.Color.White
        End If
    End Sub

    Protected Sub btn_guardar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_guardar.Click
        If ViewState("NUEVO") = "M" Then    'Si es modificación
            'Actualizamos los datos principales
            update_avance_pac(ViewState("SEG_PAC").ToString, ddlSemaforo.SelectedValue.ToString, txtAcciones.Text, txtObs.Text, ViewState("PERIODO").ToString)
            'Recorremos la grilla
            For i As Int16 = 0 To gvFechas.Rows.Count - 1
                Dim txt As TextBox = CType(gvFechas.Rows(i).FindControl("txtFecha"), TextBox)
                Dim hfCodigo As HiddenField = CType(gvFechas.Rows(i).FindControl("hfCodigo"), HiddenField)
                If Len(RTrim(LTrim(txt.Text))) = 10 Then 'Verificamos el numero exacto de caracteres
                    If txt.Enabled = True Then  'Si est'a habilitada se inserta la fecha
                        InsertarFechas(ViewState("SEG_PAC").ToString, hfCodigo.Value.ToString, txt.Text)
                    End If
                End If
            Next
            'Cargamos las fechas
            MostrarFechas(ViewState("SEG_PAC").ToString)
            alerta.Mostrar_Alerta("Aviso", "Los Datos fueron Actualizados correctamente")
        Else    'Si es nuevo
            Dim OBJGENERAL As GeneralBC
            OBJGENERAL = New GeneralBC
            Dim ID_AVANCE As String
            'Generamos el codigo para el PAC
            ID_AVANCE = OBJGENERAL.GenerarCodigoNumero("SEG_PERIODO_AVAN_PAC", "SEG_PAC")
            'Insertamos el PAC y sus datos principales
            insert_avance_pac(ID_AVANCE, ViewState("ID_PAC").ToString, ddlSemaforo.SelectedValue.ToString, txtAcciones.Text, txtObs.Text, ViewState("PERIODO").ToString)
            'Recorremos la grilla
            For i As Int16 = 0 To gvFechas.Rows.Count - 1
                Dim txt As TextBox = CType(gvFechas.Rows(i).FindControl("txtFecha"), TextBox)
                Dim hfCodigo As HiddenField = CType(gvFechas.Rows(i).FindControl("hfCodigo"), HiddenField)
                If Len(RTrim(LTrim(txt.Text))) = 10 Then 'Verificamos el numero exacto de caracteres e insertamos las fechas
                    InsertarFechas(ID_AVANCE, hfCodigo.Value.ToString, txt.Text)
                End If
            Next
            'cargamos la grilla
            MostrarFechas(ID_AVANCE)
            'capturamos el nuevo codigo
            ViewState("SEG_PAC") = ID_AVANCE
            'cambiamos el estado a modificacion
            ViewState("NUEVO") = "M"
            alerta.Mostrar_Alerta("Aviso", "Los Datos se guardaron correctamente")
        End If
    End Sub

    'Inserta PAC
    Private Sub insert_avance_pac(ByVal id_seg As String, ByVal id_pac As String, ByVal stado As String, ByVal accion As String, ByVal comentario As String, ByVal periodo As String)
        Dim objPACBC As PacBC
        Try
            objPACBC = New PacBC
            objPACBC.insert_avancepac2(id_seg, id_pac, stado, accion, comentario, periodo)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    'Insertar Fechas
    Private Sub InsertarFechas(ByVal strSeg As String, ByVal strIdFecha As String, ByVal strFecha As String)
        Dim objPACBC As PacBC
        Try
            objPACBC = New PacBC

            objPACBC.InsertarFechas(strSeg, strIdFecha, strFecha)

        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    'Actualizar PAC
    Private Sub update_avance_pac(ByVal id_seg As String, ByVal stado As String, ByVal accion As String, ByVal comentario As String, ByVal periodo As String)
        Dim objPACBC As PacBC
        Try
            objPACBC = New PacBC
            objPACBC.update_avance_pac(id_seg, stado, accion, comentario, periodo)
        Catch ex As Exception
            Throw ex
        End Try

    End Sub

    Protected Sub gvFechas_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvFechas.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then 'Recorremos las filas le damos fondo y borde
            e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(255, 255, 255)
            e.Row.Cells(1).BackColor = Drawing.Color.FromArgb(255, 255, 255)
            e.Row.Cells(0).BorderStyle = BorderStyle.None
            e.Row.Cells(1).BorderStyle = BorderStyle.None
        End If
    End Sub

    'Salir
    Protected Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCerrar.Click
        Response.Redirect("PAC.aspx")
    End Sub
End Class
